<!--
  Copyright (c) 2006-2013, JGraph Ltd
  
  Backend example for mxGraph. This example demonstrates using
  AJAX to open and save XML from a .NET backend.
-->
<html>
<head>
	<title>Backend example for mxGraph</title>

	<!-- Sets the basepath for the library if not in same directory -->
	<script type="text/javascript">
	    mxBasePath = '/mxgraph/javascript/src';
	</script>

	<!-- Loads and initializes the library -->
	<script type="text/javascript" src="/mxgraph/javascript/src/js/mxClient.js"></script>

	<!-- Example code -->
	<script type="text/javascript">
	    // Program starts here. Creates a sample graph in the
	    // DOM node with the specified ID. This function is invoked
	    // from the onLoad event handler of the document (see below).
	    function main(container) {
	        // Checks if the browser is supported
	        if (!mxClient.isBrowserSupported()) {
	            // Displays an error message if the browser is not supported.
	            mxUtils.error('Browser is not supported!', 200, false);
	        }
	        else {
	            // Disables the built-in context menu
	            mxEvent.disableContextMenu(container);

	            // Creates the graph inside the given container
	            var graph = new mxGraph(container);

	            // Opens a file from the backend
	            document.body.appendChild(mxUtils.button('Open', function () {
	                // Asks for dummy file ID to demonstrate handling of IDs in general
	                var id = mxUtils.prompt('Enter file ID');

	                mxUtils.get('Backend.ashx?id=' + encodeURIComponent(id), function (req) {
	                    var node = req.getDocumentElement();
	                    var dec = new mxCodec(node.ownerDocument);
	                    dec.decode(node, graph.getModel());

	                    // Stores ID for saving
	                    graph.id = id;
	                });
	            }));

	            // Saves a file to the backend
	            document.body.appendChild(mxUtils.button('Save', function () {
	                if (graph.id == null) {
	                    mxUtils.alert('No open file');
	                }
	                else {
	                    var enc = new mxCodec();
	                    var node = enc.encode(graph.getModel());
	                    var xml = mxUtils.getXml(node);

	                    mxUtils.post('Backend.ashx?id=' + encodeURIComponent(graph.id), 'xml=' + encodeURIComponent(xml), function () {
	                        mxUtils.alert('Saved');
	                    }, function () {
	                        mxUtils.alert('Error');
	                    });
	                }
	            }));
	        }
	    };
	</script>
</head>

<!-- Page passes the container for the graph to the program -->
<body onload="main(document.getElementById('graphContainer'))">

	<!-- Creates a container for the graph with a grid wallpaper -->
	<div id="graphContainer"
		style="position:relative;overflow:hidden;width:321px;height:241px;background:url('mxgraph/javascript/examples/editors/images/grid.gif');cursor:default;">
	</div>

    <!--Ignore this. Just checks for the /mxgraph virtual directory. -->
    <div style="border: solid 1px black;background:red;display:none;padding:10px;width:60%;" id="warning">
        <h3>Warning: Virtual Directory /mxgraph not found!</h3>
        <p>
            A virtual directory (<code>/mxgraph</code>) that points to the top-level
            directory of the mxGraph distribution is required in IIS for these examples
            to work. This virtual directory must be manually created using the IIS Manager.
        </p>
    </div>
    <script type="text/javascript">
        // Checks if the virtual directory exists by loading an image that should be in
        // the virtual directory. If the image cannot be found then a warning is shown.
        var image = new Image();
        image.onerror = function () {
            document.getElementById('warning').style.display = '';
        }
        image.src = '/mxgraph/javascript/src/images/transparent.gif';
    </script>
</body>
</html>
